package cn.tianyun.pethome.org.controller;

import cn.tianyun.pethome.annotation.PreAuthorize;
import cn.tianyun.pethome.basic.util.AjaxResult;
import cn.tianyun.pethome.basic.util.PageList;
import cn.tianyun.pethome.org.domain.Department;
import cn.tianyun.pethome.org.query.DepartmentQuery;
import cn.tianyun.pethome.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Resource
    private IDepartmentService departmentService;

    @ApiOperation(value = "新增和删除")
    @PutMapping
    @PreAuthorize(name = "部门保存",value = "department:save")
    public AjaxResult save(Department department) {
        //透传
        if (department.getId() == null) {
            //先添加
            departmentService.add(department);
            //通过传回的parent是否为空判断是不是顶级部门
            if (department.getParent() == null) {
                department.setDirPath("/" + department.getId());
            } else {
                Department father = departmentService.loadById(department.getParent().getId());
                String dirPath = father.getDirPath();
                department.setDirPath(dirPath + "/" + department.getId());
            }
            departmentService.update(department);
            return AjaxResult.creat().setSuccess(true).setMsg("添加成功");

        } else {
            //修改逻辑
            departmentService.update(department);
            return AjaxResult.creat().setSuccess(true).setMsg("修改成功");
        }
    }



    @ApiOperation(value = "通过ID删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除", value = "departement:delete")
    public AjaxResult delById(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.creat().setSuccess(true).setMsg("删除成功");
        } catch (Exception e) {
            return AjaxResult.creat().setSuccess(false).setMsg("删除失败");
        }
    }
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }
    @ApiOperation(value = "分页所有")
    @PostMapping
    @PreAuthorize(name = "部门列表", value = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query) {
        return departmentService.queryPage(query);
    }

    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public Department loadById(@PathVariable("id") Long id) {
        return departmentService.loadById(id);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchdelete(ids);
            return AjaxResult.creat().setSuccess(true).setMsg("删除成功");
        } catch (Exception e) {
            return AjaxResult.creat().setSuccess(false).setMsg("删除失败");
        }
    }
    @ApiOperation(value = "请求部门树")
    @GetMapping("/tree")
    public List<Department> queryTree() {
        return departmentService.buildTree();
    }

}
